<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/21 0021
 * Time: 09:55
 */
namespace app\caipiao\controller;
use think\Cookie;
use think\Db;
use think\Session;

class Index extends Base{
    public function _initialize()
    {
        parent::_initialize();
        header("Content-type:text/html;charset=utf-8");
        if(empty(input('session.aid'))){
            $this->redirect(url('user/login'));
        }
    }


    public function index(){

        $id=input('session.aid');
        $res=Db::table('kkty_admins')->alias('a')->where('a.id',$id)->field('nickname as adminname')->find();
        $this->assign('adminname',$res['adminname']);
        $this->assign('admin',input('session.admin'));
        return $this->fetch();
    }

    public function caipiaolist(){
    	$kjjg_data_cur = Db::name('kjjg_list')->where('czid', 62)->where('jztime', '<', time())->where('kjjg', '<>', '0')->order('jztime', 'DESC')->find();
    	$query = Db::name('kjjg_list')->where(['czid'=>62,'jztime'=>['>',time()]])->order('qihao','ASC')->find();
    	$jttime = $query['jztime']-time();
    	$cftime = Db::name('caipai_list')->where('id',62)->value('cp_fp_time');
    	$jzt = $query['jztime']?$query['jztime']:0;
    	$list=Db::name('caipiao_open')->where('czid',62)->select();

		$history = Db::name('kjjg_list')->where(['czid'=>62,'kjjg'=>['<>','0']])->order('jztime', 'DESC')->limit(10)->select();
		$history = array_map(function($v){
			$v['kjjg'] = kjjg_split($v['kjjg'],$v['czid']);
			return $v;
		},$history);
		$this->assign('history',$history);
    	$this->assign('jztime_fur',$jttime);
    	$this->assign('qihao_cur',$kjjg_data_cur['qihao']);
    	$this->assign('qihao_fur',$query['qihao']);
    	$this->assign('jzt',$jzt);
    	$this->assign('query',$query);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    public function caipiaolist_pk(){
    	$kjjg_data_cur = Db::name('kjjg_list')->where('czid', 63)->where('jztime', '<', time())->where('kjjg', '<>', '0')->order('jztime', 'DESC')->find();
    	$query = Db::name('kjjg_list')->where(['czid'=>63,'jztime'=>['>',time()]])->order('qihao','ASC')->find();
    	$jttime = $query['jztime']-time();
    	
    	$cftime = Db::name('caipai_list')->where('id',63)->value('cp_fp_time');
    	$jzt = $query['jztime']?$query['jztime']:0;
    	$list=Db::name('caipiao_open')->where('czid',63)->select();

		$history = Db::name('kjjg_list')->where(['czid'=>63,'kjjg'=>['<>','0']])->order('jztime', 'DESC')->limit(10)->select();
		$history = array_map(function($v){
			$v['kjjg'] = kjjg_split($v['kjjg'],$v['czid']);
			return $v;
		},$history);
		$this->assign('history',$history);

    	$this->assign('jztime_fur',$jttime);
    	$this->assign('qihao_cur',$kjjg_data_cur['qihao']);
    	$this->assign('qihao_fur',$query['qihao']);
    	$this->assign('jzt',$jzt);	
    	$this->assign('query',$query);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //五分六合彩
    public function caipiaolist_wf(){
        $kjjg_data_cur = Db::name('kjjg_list')->where('czid', 73)->where('jztime', '<', time())->where('kjjg', '<>', '0')->order('jztime', 'DESC')->find();
        $query = Db::name('kjjg_list')->where(['czid'=>73,'jztime'=>['>',time()]])->order('qihao','ASC')->find();
        $jttime = $query['jztime']-time();
        
        $cftime = Db::name('caipai_list')->where('id',73)->value('cp_fp_time');
        $jzt = $query['jztime']?$query['jztime']:0;
        $list=Db::name('caipiao_open')->where('czid',73)->select();

		$history = Db::name('kjjg_list')->where(['czid'=>73,'kjjg'=>['<>','0']])->order('jztime', 'DESC')->limit(10)->select();
		$history = array_map(function($v){
			$v['kjjg'] = kjjg_split($v['kjjg'],$v['czid']);
			return $v;
		},$history);
		$this->assign('history',$history);

        $this->assign('jztime_fur',$jttime);
        $this->assign('qihao_cur',$kjjg_data_cur['qihao']);
        $this->assign('qihao_fur',$query['qihao']);
        $this->assign('jzt',$jzt);  
        $this->assign('query',$query);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function new_kaijian(){
    	$czid=input('czid');
    	$qihao = input('post.qihao/a');
    	$kjjg = input('post.num/a');
    	$kjjg = array_filter($kjjg);
    	$arr = array();
        $id=input('session.aid');
        $res=Db::table('kkty_admins')->alias('a')->where('a.id',$id)->field('nickname as adminname')->find();
        $cp = Db::name('caipai_list')->where('id',$czid)->find();
    	if(!empty($kjjg)){
	    	foreach ($kjjg as $k=>$v){
	    		$arr[$qihao[$k]]=$v;
	    	}
	    	foreach ($arr as $key=>$v){
	    		$data=array('qihao'=>$key,'kjjg'=>$v,'czid'=>$czid,'iftz'=>0);
	    		$query = Db::name('caipiao')->where(['czid'=>$czid,'qihao'=>$key])->find();
	    		if($query){
	    			Db::name('caipiao')->where('id',$query['id'])->update($data);
                    
	    		}else{
	    			Db::name('caipiao')->insert($data);
	    		}
                $data=array('user'=>$res['adminname'],'jilv'=>$cp['cp_name_old'].'期号：'.$key.'设置开奖结果','time'=>time(),'ip'=>get_ip());
                Db::name('caipiao_jl')->insert($data);
	    	}
	    	alert_back('成功');
    	}else{
    		alert_back('开奖结果不能全为空');
    	}
    	
    }
    public function timeIntval(){
    	$uid = input('post.uid');
    	$jztime = input('jzt');
    	$qihao_old = input('qihao');
    	$dqtime = array();
    	$time = Db::name('caipai_list')->where('id',$uid)->field('time,cp_fp_time')->find();
    	$tm = date('H:i:s',time()+$time['cp_fp_time']);
    	
    	$sj = Db::name('time')->where('czid',$uid)->where('jztime','>',$tm)->order('jztime','ASC')->limit(10)->select();
    	$time_min = Db::name('time')->field('MIN(qihao),czid')->group('czid')->column('qihao,jztime','czid');
    	$time_min_xyft = Db::name('time')->where(['czid'=>61])->field('qihao,jztime')->order("jztime ASC")->find();
    	$xyft_time_qh = Db::name('time')->where(['czid'=>61])->column("jztime,qihao");
    	foreach ($sj as $key=>$val){
    		
    		$sz_time = strtotime(date('Y-m-d') . ' ' . $val['jztime']);
    		switch ( intval($uid) ) {
    			
    			case 62:
    				if ($val['qihao'] == 0 && $val['jztime'] = '00:00:00') {
    					$qihao = date('YmdHi', $sz_time);
    				} else {
    					$qihao = date('YmdHi', strtotime(date('Y-m-d') . $val['jztime']));
    				}
    				break;
    			case 63:
    
    			case 72:
    				if($val['qihao']==1440 && $data['jztime']='00:00:00') {
    					$qihao = date('Ymd',strtotime('-1 day')).$val['qihao'];
    				}else{
    					if(strlen($val['qihao'])==1){$val['qihao'] = '000'.$val['qihao'];}
    					if(strlen($val['qihao'])==2){$val['qihao'] = '00'.$val['qihao'];}
    					if(strlen($val['qihao'])==3){$val['qihao'] = '0'.$val['qihao'];}
    					$qihao = date('Ymd').$val['qihao'];
    				}
    				break;
                case 73: //五分六合彩
                    if ($val['qihao'] < 10) $val['qihao'] = '00' . $val['qihao'];
                    if ($val['qihao'] >= 10 && $val['qihao'] < 100) $val['qihao'] = '0' . $val['qihao'];
                    $qihao = date('Ymd', $sz_time) . $val['qihao'];
                    break;
    			
    		}
    
    		$dqtime[] = array('jgtime'=>$time['cp_fp_time'],'time'=>$sz_time-$time['cp_fp_time'],'fptime'=>$sz_time-$time['cp_fp_time']-$jztime,'qihao'=>$qihao,'jzt'=>$jztime);
    
    	}
    	//$shijian = $dqtime-time();
    	return json_encode($dqtime,JSON_UNESCAPED_UNICODE);
    }
    
    public function caipiaolist_ks(){
    	$kjjg_data_cur = Db::name('kjjg_list')->where('czid', 72)->where('jztime', '<', time())->where('kjjg', '<>', '0')->order('jztime', 'DESC')->find();
    	$query = Db::name('kjjg_list')->where(['czid'=>72,'jztime'=>['>',time()]])->order('qihao','ASC')->find();
    	$jttime = $query['jztime']-time();
    	
    	$cftime = Db::name('caipai_list')->where('id',72)->value('cp_fp_time');
    	$jzt = $query['jztime']?$query['jztime']:0;
    	$list=Db::name('caipiao_open')->where('czid',72)->select();

		$history = Db::name('kjjg_list')->where(['czid'=>72,'kjjg'=>['<>','0']])->order('jztime', 'DESC')->limit(10)->select();
		$history = array_map(function($v){
			$v['kjjg'] = kjjg_split($v['kjjg'],$v['czid']);
			return $v;
		},$history);
		$this->assign('history',$history);

    	$this->assign('jztime_fur',$jttime);
    	$this->assign('qihao_cur',$kjjg_data_cur['qihao']);
    	$this->assign('qihao_fur',$query['qihao']);
    	$this->assign('jzt',$jzt);
    	$this->assign('query',$query);
    	$this->assign('list',$list);
    	return $this->fetch();
    }

    public function change(){
        $id = input('post.id');
        //echo $id;die;
        $id=intval($id);
        //dump($id);die;
        $res=Db::table('kkty_caipiao_open')->where('id',$id)->find();
        $uid=input('session.aid');
        $query=Db::table('kkty_admins')->alias('a')->where('a.id',$uid)->field('nickname as adminname')->find();
        //dump($res);die;
        if($res['filter']==1){
            $res1=Db::table('kkty_caipiao_open')->update(array('id'=>$id,'filter'=>0));
            $data=array('user'=>$query['adminname'],'jilv'=>$res['cname'].$res['wanfa'].'玩法杀率关闭','time'=>time(),'ip'=>get_ip());
            Db::name('caipiao_jl')->insert($data);
        }elseif($res['filter']==0){
            $res1=Db::table('kkty_caipiao_open')->update(array('id'=>$id,'filter'=>1));
            $data=array('user'=>$query['adminname'],'jilv'=>$res['cname'].$res['wanfa'].'玩法杀率开启','time'=>time(),'ip'=>get_ip());
            Db::name('caipiao_jl')->insert($data);
        }
        //dump($res1);die;
        if($res1==1){
            return true;
        }else{
            return false;
        }
    }

    public function logout(){

        Session::set('auth','');
        Session::set('admin','');
        Session::set('aid','');
        $this->redirect('/caipiao/user/login');
    }
	public function latestCode(){
		$czid = input('uid',0);
		$latest = Db::name('kjjg_list')->field('qihao,kjjg')->where(['czid'=>$czid,'kjjg'=>['<>','0']])->order('jztime', 'DESC')->find();
		if(empty($latest)){
			return json_encode(['status'=>0],JSON_UNESCAPED_UNICODE);
		}else{
			$latest['status'] = 1;
			$latest['kjjg'] = kjjg_split($latest['kjjg'],$czid);
		}
		return json_encode($latest,JSON_UNESCAPED_UNICODE);
	}
}